/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.ipo.mm.ejb.facade.meeting;

import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import org.ipo.mm.ejb.entities.Meeting;
import org.ipo.mm.ejb.util.MeetingSearchEJBQLQueryBuilder;

/**
 *
 * @author makcro
 */
@Stateless
public class MeetingFacade implements MeetingFacadeLocal, MeetingFacadeRemote {

    @PersistenceContext
    private EntityManager em;

    public void create(Meeting meeting) {
        em.persist(meeting);
        em.flush();

    }

    public void edit(Meeting meeting) {
        em.merge(meeting);
        em.flush();
    }

    public void remove(Meeting meeting) {
        em.remove(em.merge(meeting));
    }

    public Meeting find(Object id) {
        return em.find(Meeting.class, id);
    }

    public List<Meeting> findAll() {
        return em.createNamedQuery("Meeting.findAll").getResultList();
    }






//    public void createWithDoc(Meeting meeting, Vector<Document> docs) {
//
//        em.persist(meeting);
//
//        for (Document document : docs) {
//            DocumentPK documentPK = new DocumentPK(0, meeting.getId());
//            document.setDocumentPK(documentPK);
//            em.persist(document);
//        }
//
//    }
    public void markDeleted(int meetingId) {
        Query q = em.createNamedQuery("Meeting.markDeleted");
        q.setParameter("id_param", meetingId);

        q.executeUpdate();
    }

    public void refresh(Meeting meeting) {

        em.refresh(em.merge(meeting));
    }

    public Meeting findFetchDocs(int meetingId) {
        Query q = em.createNamedQuery("Meeting.findByIdFetchDocs");
        q.setParameter("id", meetingId);
//        List<Meeting> resutls = q.getResultList();
//        return resutls.get(0);
        Meeting result = (Meeting) q.getSingleResult();
        return result;
    }

    public Collection<Meeting> search(
            String meetingNameFilter,
            String meetingNameOper,
            String venueFilter,
            String venueOper,
            String conclutionsFilter,
            String conclutionsOper,
            Date startDate,
            Date endDate,
            String orgNameFilter,
            String orgNameOper,
            String[] orgInstFilter,
            String[] orgITypeFilter,
            String[] orgIFamilyFilter,
            String[] orgIRegionFilter,
            String[] orgIdFilter,
            String repNameFilter,
            String repNameOper,
            String[] repInstFilter,
            String[] repITypeFilter,
            String[] repIFamilyFilter,
            String[] repIRegionFilter,
            String[] repIdFilter,
            String orderByFilter,
            int firstResult,
            int maxResults,
            boolean isQuickSearch) {


//        MeetingSearchQueryBuilder msqb = new MeetingSearchQueryBuilder(meetingNameFilter,
//                meetingNameOper,
//                venueFilter,
//                venueOper,
//                conclutionsFilter,
//                conclutionsOper,
//                dateInterval,
//                orgNameFilter,
//                orgNameOper,
//                orgInstFilter,
//                orgITypeFilter,
//                orgIFamilyFilter,
//                orgIRegionFilter,
//                repNameFilter,
//                repNameOper,
//                repInstFilter,
//                repITypeFilter,
//                repIFamilyFilter,
//                repIRegionFilter,
//                orderByFilter,
//                false);

        boolean putDateIntervalClause = checkDates(startDate, endDate);


        MeetingSearchEJBQLQueryBuilder builder = new MeetingSearchEJBQLQueryBuilder(
                meetingNameFilter,
                meetingNameOper,
                venueFilter,
                venueOper,
                conclutionsFilter,
                conclutionsOper,
                putDateIntervalClause,
                orgNameFilter,
                orgNameOper,
                orgInstFilter,
                orgITypeFilter,
                orgIFamilyFilter,
                orgIRegionFilter,
                orgIdFilter,
                repNameFilter,
                repNameOper,
                repInstFilter,
                repITypeFilter,
                repIFamilyFilter,
                repIRegionFilter,
                repIdFilter,
                orderByFilter,
                false,
                isQuickSearch);

        String queryString = builder.getQuery();
//        String queryS = "SELECT m FROM Meeting m JOIN FETCH m.documentCollection " +
//                " WHERE " +
//                " m.organizer.institution.id IN (1,2,3,4,5,6,7,8,9,10) " +
//                " ORDER BY m.date ";


        Query query = em.createQuery(queryString);

        if (putDateIntervalClause) {
            query.setParameter("startDate", startDate);
            query.setParameter("endDate", endDate);
        }

        //in case of quick search
        if (maxResults != -1 && firstResult != -1) {
            query.setFirstResult(firstResult);
            query.setMaxResults(maxResults);
        }


        List<Meeting> result = query.getResultList();
        int i;
        for (Iterator<Meeting> it = result.iterator(); it.hasNext();) {
            Meeting meeting = it.next();
            i = meeting.getRepresentativeCollection().size();
        }
        return result;
    }

    public Long getSearchResultsCount(
            String meetingNameFilter,
            String meetingNameOper,
            String venueFilter,
            String venueOper,
            String conclutionsFilter,
            String conclutionsOper,
            Date startDate,
            Date endDate,
            String orgNameFilter,
            String orgNameOper,
            String[] orgInstFilter,
            String[] orgITypeFilter,
            String[] orgIFamilyFilter,
            String[] orgIRegionFilter,
            String[] orgIdFilter,
            String repNameFilter,
            String repNameOper,
            String[] repInstFilter,
            String[] repITypeFilter,
            String[] repIFamilyFilter,
            String[] repIRegionFilter,
            String[] repIdFilter,
            String orderByFilter,
            boolean isQuickSearch) {

//        MeetingSearchQueryBuilder msqb = new MeetingSearchQueryBuilder(meetingNameFilter,
//                meetingNameOper,
//                venueFilter,
//                venueOper,
//                conclutionsFilter,
//                conclutionsOper,
//                dateInterval,
//                orgNameFilter,
//                orgNameOper,
//                orgInstFilter,
//                orgITypeFilter,
//                orgIFamilyFilter,
//                orgIRegionFilter,
//                repNameFilter,
//                repNameOper,
//                repInstFilter,
//                repITypeFilter,
//                repIFamilyFilter,
//                repIRegionFilter,
//                orderByFilter,
//                true);


        boolean putDateIntervalClause = checkDates(startDate, endDate);

        MeetingSearchEJBQLQueryBuilder builder = new MeetingSearchEJBQLQueryBuilder(
                meetingNameFilter,
                meetingNameOper,
                venueFilter,
                venueOper,
                conclutionsFilter,
                conclutionsOper,
                putDateIntervalClause,
                orgNameFilter,
                orgNameOper,
                orgInstFilter,
                orgITypeFilter,
                orgIFamilyFilter,
                orgIRegionFilter,
                orgIdFilter,
                repNameFilter,
                repNameOper,
                repInstFilter,
                repITypeFilter,
                repIFamilyFilter,
                repIRegionFilter,
                repIdFilter,
                orderByFilter,
                true,
                isQuickSearch);




        String queryString = builder.getQuery();
        Query query = em.createQuery(queryString);

        if (putDateIntervalClause) {
            query.setParameter("startDate", startDate);
            query.setParameter("endDate", endDate);
        }



        return (Long) query.getSingleResult();


    }

    private boolean checkDates(Date startDate, Date endDate) {
        return startDate != null && endDate != null;
    }
}
